<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/12/2
 * @Description: 用户消息表
 */

namespace App\Models;


use Illuminate\Database\Eloquent\SoftDeletes;

class ImNotification extends BaseModel
{
    use SoftDeletes;

    protected $table = 'im_notification';

    /**
     * 关联用户表
     */
    public function user()
    {
        return $this->belongsTo(ImUsers::class, 'opera_user_id', 'id')
            ->select(['id', 'nickname', 'headimg']);
    }

    /**
     * 关联评论表
     */
    public function comment()
    {
        return $this->belongsTo(ImFriendsCircleComments::class, 'comment_id', 'id')
            ->select(['id', 'friend_id', 'content', 'to_id', 'pid']);
    }

    //点赞动态表
    public function circle()
    {
        return $this->belongsTo(ImFriendsCircle::class, 'circle_id', 'id')
        ->select(['id', 'content', 'type']);
    }



    /**
     * @desc 增加用户消息表
     * @param string $uid   通知人用户id
     * @param string $subType   类型
     * @param string $operaUserId   操作人id
     * @return array
     * @author zhouzhou 2021-12-16
     */
    public function addMessage($uid, $subType, $operaUserId, $circleId, $title, $commentId = 0)
    {
        $insert = [
            'user_id' => $uid,
            'sub_type' => $subType,
            'opera_user_id' => $operaUserId,
            'circle_id' => $circleId,
            'comment_id' => $commentId,
            'title' =>$title ,
            'created_at' => time(),
            'updated_at' => time()
        ];

        return  $this->insert($insert);
    }

    /**
     * @desc 消息通知
     * @param int $userId 用户id
     * @param int $limit 每页默认10个
     * @param int $offset 从那条数据开始
     * @return mixed
     * @author zhouzhou
     * @time 2021/12/16 16:50
     */
    public function noticeList($userId = 0, $limit = 10, $offset = 0)
    {
        $list = $this->select('id', 'user_id', 'sub_type', 'opera_user_id', 'circle_id', 'comment_id', 'title', 'created_at')->with(['user','comment','circle','circle.imgs'])->where(['user_id'=>$userId])
            ->orderBy('id', 'desc')
            ->limit($limit)
            ->offset($offset)
            ->get();

        return $list;
    }

    /**
     * @desc 删除消息通知
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/12/16 14:58
     */
    public function delNotice($userId = 0)
    {
        $this->where(['user_id' => $userId])->delete();

        return ['code' => 0, 'msg' => '删除成功'];
    }

}
